package cn.rengy.web.demo.mangodb;

import cn.rengy.isql.Dao;
import cn.rengy.lang.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ResponseBody
@RequestMapping(produces = { "application/json;charset=UTF-8"})
public class DemoMangodbController {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    Dao dao;

    /**
     * 批量插入
     * @return
     */
    @RequestMapping(value="/demo/initSysSql")
    public ResultEntity initSysSql(){
        List<Map<String,Object>> list= dao.queryForList("select * from sys_sql");
        mongoTemplate.insert(list,"sys_sql");
        return ResultEntity.ok();
    }

    /**
     * 查询一条
     * @return
     */
    @RequestMapping(value="/demo/findOne")
    public ResultEntity findOne(){
        Query query = new Query(Criteria.where("cacheType").is("redis"));
        Map map=mongoTemplate.findOne(query,Map.class,"sys_sql");
        Object id=map.get("_id");
        System.out.println(id.getClass().getName().toString());
        map.put("long",new Long(1231231));
        return ResultEntity.ok(map);
    }
    /**
     * 指定查询条件查询、排序、指定返回
     * $lt: 存在并小于
     * $lte: 存在并小于等于
     * $gt: 存在并大于
     * $gte: 存在并大于等于
     * $ne: 不存在或存在但不等于
     * $in: 存在并在指定数组中
     * $nin: 不存在或不在指定数组中
     * $or: 匹配两个或多个条件中的一个
     * $and: 匹配全部条件
     * @return
     */
    @RequestMapping(value="/demo/testQuery")
    public ResultEntity testQuery(){
        Query query = new Query(Criteria.where("cacheType").is("redis"));
        query.with(Sort.by(Sort.Direction.ASC, "sqlName")); // 按fieldName字段升序排序
        // 如果你想按多个字段排序，可以这样做:
        // query.with(Sort.by(Sort.Direction.ASC, "fieldName1").and(Sort.Direction.DESC, "fieldName2"));
        // 只返回'name'和'age'字段
        query.fields().include("id").include("sqlName").include("sqlValue");
        // 如果你想排除某些字段，可以使用exclude方法
        // query.fields().exclude("_id");
        List<Map> list=mongoTemplate.find(query,Map.class,"sys_sql");
        return ResultEntity.ok(list);
    }


    /**
     * 普通分页查询
     * @return
     */
    @RequestMapping(value="/demo/testPage")
    public ResultEntity testPage(@RequestParam int pageNumber,@RequestParam int pageSize){
        Query query = new Query(Criteria.where("cacheType").is("redis"));
        pageNumber = pageNumber < 1 ? 1  : pageNumber;
        pageSize   = pageSize   < 1 ? 10 : pageSize;
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        query.with(pageRequest);
        List<Map> list=mongoTemplate.find(query,Map.class,"sys_sql");
        return ResultEntity.ok(list);
    }

    /**
     * 高效分页查询
     * @return
     */
    @RequestMapping(value="/demo/testPage2")
    @ResponseBody
    public ResultEntity testPage2(@RequestParam int pageNumber,@RequestParam int pageSize,@RequestParam(required = false) String lastId){
        Criteria criteria=Criteria.where("cacheType").is("redis");
        if(lastId!=null){
            criteria.and("_id").gt(lastId);
        }
        Query query = new Query(criteria);//大于最后一个id
        query.with(Sort.by(Sort.Direction.ASC, "_id")); //正序返回
        query.fields().include("id").include("sqlName").include("sqlValue");
        query.limit(pageSize);
        List<Map> list=mongoTemplate.find(query,Map.class,"sys_sql");
        return ResultEntity.ok(list);
    }

    @RequestMapping(value="/demo/update")
    public ResultEntity update(@RequestParam(required = false) String _id){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(_id));
        Update update = new Update();
        update.set("sqlValue", "sqlValue");
        mongoTemplate.updateFirst(query,update,"sys_sql");
        return ResultEntity.ok();
    }

    @RequestMapping(value="/demo/test")
    public ResultEntity test(@RequestBody(required = false) Map<String,Object> param){
        Map<String,Object> map=new HashMap<>();
        map.put("a","a");
        mongoTemplate.insert(map,"test");
        return ResultEntity.ok();
    }
}
